package ms.platform.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	private static final Logger logger = LoggerFactory
			.getLogger(DateUtil.class);

	public final static String FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

	public final static String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";

	public final static String FORMAT_YYYYMMDD = "yyyyMMdd";

	/**
	 * 根据指定日期获得前几天的日期
	 *
	 * @param date
	 * @return
	 */
	public static String getPreviousDate(Date date, int previous, String aMask) {
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_MONTH, -previous);
			SimpleDateFormat formatter = new SimpleDateFormat(aMask);
			String time = formatter.format(calendar.getTime());
			return time;
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}

	}

	/**
	 * 根据指定日期获得后几天的日期
	 *
	 * @param date
	 * @return
	 */
	public static String getAfterDate(Date date, int previous, String aMask) {
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DAY_OF_MONTH, +previous);
			SimpleDateFormat formatter = new SimpleDateFormat(aMask);
			String time = formatter.format(calendar.getTime());
			return time;
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}

	}

	public static String getDiffDate(Date date, int previous, String aMask) {
		try {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(Calendar.DATE, previous);
			SimpleDateFormat formatter = new SimpleDateFormat(aMask);
			String time = formatter.format(calendar.getTime());
			return time;
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}

	}

	/**
	 * 返回指定格式的日期字符串
	 *
	 * @param aMask
	 * @param aDate
	 * @return
	 */
	public static final String getDateString(Date aDate, String aMask) {
		SimpleDateFormat df = null;
		String returnValue = "";

		if (aDate == null) {
			logger.debug("aDate is null!");
		} else {
			df = new SimpleDateFormat(aMask);
			returnValue = df.format(aDate);
		}

		return (returnValue);
	}

	public static String getNowTime() {
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_YYYY_MM_DD_HH_MM_SS);
		String format = sdf.format(date);
		return format;

	}

	/**
	 * 将长时间格式字符串转换为时间
	 *
	 * @param strDate
	 * @return
	 */
	public static Date convertStringToDate(String strDate, String aMask) {

		try {
			SimpleDateFormat formatter = new SimpleDateFormat(aMask);
			Date date = formatter.parse(strDate);
			return date;
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		}
	}

	/**
	 * 获取当前日期的下一天 时间为00:00:00
	 *
	 * @return
	 */
	public static Date getNextDateTime(Date currentDate) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(currentDate);
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0);
		cal.add(Calendar.DAY_OF_MONTH, 0);
		cal.add(Calendar.DATE, 1);
		return cal.getTime();
	}

	/**
	 * 增加日期的年份。失败返回null。
	 *
	 * @param date
	 *            日期
	 * @param yearAmount
	 *            增加数量。可为负数
	 * @return 增加年份后的日期
	 */
	public static Date addYear(Date date, int yearAmount) {
		return addInteger(date, Calendar.YEAR, yearAmount);
	}

	public static Date addMonth(Date date, int monthAmount) {
		return addInteger(date, Calendar.MONTH, monthAmount);
	}

	public static Date addDate(Date date, int dateAmount) {
		return addInteger(date, Calendar.DATE, dateAmount);
	}

	/**
	 * 增加日期中某类型的某数值。如增加日期
	 *
	 * @param date
	 *            日期
	 * @param dateType
	 *            类型
	 * @param amount
	 *            数值
	 * @return 计算后日期
	 */
	private static Date addInteger(Date date, int dateType, int amount) {
		Date myDate = null;
		if (date != null) {
			Calendar calendar = Calendar.getInstance();
			calendar.setTime(date);
			calendar.add(dateType, amount);
			myDate = calendar.getTime();
		}
		return myDate;
	}

	/**
	 * 根据相应的格式初始化日期格式对象
	 *
	 * @param pattern
	 * @return
	 */
	public static DateFormat getDateFormat(final String pattern) {
		return new SimpleDateFormat(pattern);
	}

	public static String birthday(String birthday) {
		StringBuffer bir = new StringBuffer();
		int length = birthday.length();
		if (length == 18) {
			int i = 6;// 截取字符的起点
			String year = birthday.substring(i, i + 4);// 截取年
			String yue = birthday.substring(i + 4, i + 6);// 截取月
			String day = birthday.substring(i + 6, i + 8);// 截取日
			bir.append(year + "-");
			bir.append(yue + "-");
			bir.append(day);
		} else {
			int i = 6;// 截取的起点
			String year = birthday.substring(i, i + 2);// 截取年
			String yue = birthday.substring(i + 2, i + 4);// 截取月
			String day = birthday.substring(i + 4, i + 6);// 截取日
			bir.append("19" + year + "-");
			bir.append(yue + "-");
			bir.append(day);
		}
		return bir.toString();
	}

	public static long getCurrentTime() {
		Date date = new Date();
		long time = date.getTime();
		return time;
	}

	/**
	 * 获取现在时间
	 *
	 * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
	 */
	public static Date getNowDate() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		ParsePosition pos = new ParsePosition(0);
		Date currentTime_2 = formatter.parse(dateString, pos);
		return currentTime_2;
	}

	/**
	 * 日期相减得到天数
	 *
	 * @param startDate
	 * @param endDate
	 * @return
	 */
	public static long getDayByDateSubraction(Date endDate, Date startDate) {
		long day = (endDate.getTime() - startDate.getTime())
				/ (24 * 60 * 60 * 1000);
		return day + 1;
	}

	/**
	 * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
	 *
	 * @param nowTime
	 *            当前时间
	 * @param startTime
	 *            开始时间
	 * @param endTime
	 *            结束时间
	 * @return
	 */
	public static boolean isEffectiveDate(Date nowTime, Date startTime,
			Date endTime) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
		String nowDate = df.format(nowTime);
		String startDate = df.format(startTime);
		String endDate = df.format(endTime);
		if (nowDate.equals(startDate) || nowDate.equals(endDate)) {
			return true;
		}
		Calendar date = Calendar.getInstance();
		date.setTime(nowTime);

		Calendar begin = Calendar.getInstance();
		begin.setTime(startTime);

		Calendar end = Calendar.getInstance();
		end.setTime(endTime);

		if (date.after(begin) && date.before(end)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 比较日期大小
	 *
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int compare_date(String date1, String date2) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			Date dt1 = df.parse(date1);
			Date dt2 = df.parse(date2);
			if (dt1.getTime() > dt2.getTime()) {
				return 1;
			} else if (dt1.getTime() < dt2.getTime()) {
				return -1;
			} else {
				return 0;
			}
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return 0;
	}

	// 获取时间差
	public static Long middate(String newDate, String date,
			SimpleDateFormat format) {
		long day = 0;
		try {
			Date endDate = format.parse(newDate);// 当前时间
			Date beginDate = format.parse(date);// 更新时间
			day = (endDate.getTime() - beginDate.getTime())
					/ (24 * 60 * 60 * 1000);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return day;
	}

	public static int dateToWeek(String datetime) {
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd");
		String[] weekDays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
		Calendar cal = Calendar.getInstance(); // 获得一个日历
		Date datet = null;
		try {
			datet = f.parse(datetime);
			cal.setTime(datet);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		int w = cal.get(Calendar.DAY_OF_WEEK) - 1; // 指示一个星期中的某天。
		if (w < 0) {
			w = 0;
		}
		return w;
	}

	/**
	 * 判断日期是星期几
	 * 
	 * @param pTime
	 *            修要判断的时间
	 * @return dayForWeek 判断结果
	 * @Exception 发生异常
	 */
	public static int dayForWeek(String pTime){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance();
		try {
			c.setTime(format.parse(pTime));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int dayForWeek = 0;
		if (c.get(Calendar.DAY_OF_WEEK) == 1) {
			dayForWeek = 7;
		} else {
			dayForWeek = c.get(Calendar.DAY_OF_WEEK) - 1;
		}
		return dayForWeek;
	}
}
